﻿using AspNetCoreRateLimit;
using Microsoft.Extensions.Configuration;
using Zhp.Common.Helper;

namespace Zhp.Api.Extensions
{
    public static class IpRateLimitSetup
    {
        public static void AddIpRateLimit(this IServiceCollection services)
        {
            services.AddMemoryCache();

            services.Configure<IpRateLimitOptions>(AppHelper.Configuration.GetSection("IpRateLimiting"));

            services.Configure<IpRateLimitPolicies>(AppHelper.Configuration.GetSection("IpRateLimitPolicies"));

            //services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
            //services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
            //services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();

            services.AddInMemoryRateLimiting();

            // the clientId/clientIp resolvers use it.
            // services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
        }

        public static void UseIpRateLimit(this IApplicationBuilder app)
        {
            app.UseIpRateLimiting();
        }
    }
}
